<?php

namespace App\Http\Controllers;

use App\Services\OSS;
use Illuminate\Http\Request;

class ApiController extends Controller
{
    use \App\Traits\ApiTrait, \App\Traits\SessionTrait, \App\Traits\AjaxTrait, \App\Traits\DbTrait, \App\Traits\UserTrait;

    private $_requestData;

    public function __construct(Request $request)
    {
        $this->_requestData = $request->all();
    }

    /**
     * 文件上传
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function uploadFile(Request $request)
    {
        $file = $request->file('files');
        $fileExtension = $file->getClientOriginalExtension();
        $fileOriginalName = $file->getClientOriginalName();
        list($time1, $time2) = explode(' ', microtime());
        $microTime = (float) $time2 + (float) $time1;
        $newName = $microTime . "." . $fileExtension;
        $ossConfigData = config('oss.' . config('oss.default'));
        if (in_array($fileExtension, $ossConfigData['img_ext'])) {
            $bucketName = $ossConfigData['image_bucket'];
        } else {
            $bucketName = $ossConfigData['file_bucket'];
        }
        //先放到服务器，再上传到OSS
        $localPath = public_path() . '/uploads';
        $file->move($localPath, $newName);

        $ossPath = 'tmpfile/' . $newName;

        $result = OSS::publicUpload($bucketName, $ossPath, $localPath . '/' . $newName);
        $ossUrl = OSS::getPublicUploadUrl($bucketName, $ossPath);

        if ($result) {
            echo json_encode(['name' => $fileOriginalName, 'url' => $ossUrl]);
        } else {
            echo json_encode(['result' => 999, 'msg' => '上传错误']);
        }
    }

    /**
     * 发送短信验证码
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function pushCode(Request $request)
    {
        $postData = $this->ajaxSendSms($request);
        $postData['code'] = mt_rand(100000, 999999);
        $code=$postData['code'];
        $postData['ip'] = $request->ip();
        $postData['type'] = 1;
        $result = $this->dbAddSmsCodeLog($postData);
        if ($result['result'] == 0) {
            if (env('DB_CONNECTION') == 'product_mysql') {
                $result2 = $this->pushCodeTrait(parent::CODE_SEND_SMS, $postData);
                $result['code']=$code;
            } else {
                $result2 = true;
                $result['code']=$code;
            }
            $result = $result2 ? $result : ['result' => -1, 'msg' => '短信发送失败，请重试'];
        }
        echo json_encode($result);
    }

    /**
     * 邮箱认证
     * @return [type] [description]
     */
    public function emailAuth()
    {
        $data = ['md5' => $this->_requestData['flag'], 'email' => $this->_requestData['source']];
        $tableData = $this->validateEmailFlag($data);
        if (!$tableData['id']) {
            $viewData = ['seo' => $this->returnSeoInfo('tanwin_index'), 'validate_result' => false];
            return view('web.company.email_auth_result', $viewData);
        }
        $this->validateEmailFlagSuccess($tableData);

        $userInfo = [
            'account_phone' => $tableData['phone'],
            'new_pwd' => mt_rand(10000000, 99999999) . 'abc',
        ];
        $this->userForgetPwdTrait(parent::CODE_UPDATE_USER_PWD, $userInfo);

        $mailInfo = [
            'mail_address' => $tableData['email'],
            'mail_subject' => '碳银网帐号信息' . time(),
            'mail_content' => '您登录碳银网的密码为:' . $userInfo['new_pwd'],
        ];
        $this->pushEmailData(parent::CODE_SEND_EMAIL, $mailInfo);
        $viewData = ['seo' => $this->returnSeoInfo('tanwin_index'), 'validate_result' => true];
        return view('web.company.email_auth_result', $viewData);
    }
}
